package com.sx.zongzhi.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.calender.listener.OnPagerChangeListener;
import com.sx.zongzhi.calender.utils.CalendarUtil;
import com.sx.zongzhi.calender.weiget.CalendarView;
import com.sx.zongzhi.presenter.CheckInPest;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.CheckInView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 签到
 * Created by admin on 2017/12/29.
 */

public class CheckInActivity extends BaseActivity implements CheckInView {
    @BindView(R.id.tv_month_count)
    TextView tvMonthCount;
    @BindView(R.id.calendar)
    CalendarView calendar;
    @BindView(R.id.tv_current_date)
    TextView tvCurrentDate;
    private boolean isCurrent = true;
    private int year,month;

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_checkin);
        ButterKnife.bind(this);
        setTitleName("签到");
        setTitleRight("说明");
        int[] currentDate = CalendarUtil.getCurrentDate();
        year = currentDate[0];
        month = currentDate[1];
        new CheckInPest(CheckInActivity.this).setCheckIn(CheckInActivity.this);
    }

    @Override
    public void showError(String message) {
        if ("签到失败".equals(message)){
            getCheckList();
        }else {
            ToastUtils.showToast(CheckInActivity.this,message);
        }
    }

    @Override
    public void setCheckInSuccess(Boolean data) {
        ToastUtils.showToast(CheckInActivity.this,"签到成功");
        getCheckList();//当前
    }

    private void getCheckList(){
        new CheckInPest(CheckInActivity.this).getCheckIn(CheckInActivity.this,year,month);
    }

    @Override
    public void getCheckInListSuccess(List<String> data) {
        if (isCurrent){//第一次进来获取当前签到月
            initView(data);
        }else {
            if (data != null && data.size() > 0){
                List<String> selectDate = new ArrayList<>();
                for (String str : data){
                    String[] s =  str.split("-");
                    selectDate.add(s[0]+"."+s[1]+"."+s[2]);
                }
                calendar.refreshMonthView(selectDate);
            }
        }
    }

    private void initView(List<String> data) {
        if (data != null && data.size() > 0){
            tvMonthCount.setText("本月签到"+data.size()+"天");
            changeText(tvMonthCount);
            int[] currentDate = CalendarUtil.getCurrentDate();
            List<String> selectDate = new ArrayList<>();
            for (String str : data){
                String[] s =  str.split("-");
                selectDate.add(s[0]+"."+s[1]+"."+s[2]);
            }
            calendar.setInitDate(currentDate[0] + "." + currentDate[1])
                    .setDefaultDate(selectDate)
                    .init();
            tvCurrentDate.setText(currentDate[0] + "年" + currentDate[1] + "月");
        }
        calendar.setOnPagerChangeListener(new OnPagerChangeListener() {
            @Override
            public void onPagerChanged(int[] date) {
                tvCurrentDate.setText(date[0] + "年" + date[1] + "月");
                year = date[0];
                month = date[1];
                isCurrent = false;
                getCheckList();
            }
        });
    }

    private void changeText(TextView tv) {
        Spannable span = new SpannableString(tv.getText());
        span.setSpan(new AbsoluteSizeSpan(51), 4, tv.getText().length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        span.setSpan(new ForegroundColorSpan(Color.BLACK), 4, tv.getText().length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv.setText(span);
    }

}

